package com.topscomm.mpm;

import com.topscomm.basic.BasicEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.ICboApprovalNodeService;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.mis.MisScript;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.I18nUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.*;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.TapSystemConst.TapApprovalNodeStateEnum;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author: fanzhiming
 * @date: 2017年3月9日下午3:20:41
 * @description:
 * @modify:
 */
public class MpmScript extends MisScript {

    @Autowired
    private ICboMessageAppService cboMessageAppService;
    @Autowired
    private ICboMessageSysService cboMessageSysService;
    @Autowired
    private CboApprovalNodeInsService cboApprovalNodeInsService;
    @Autowired
    private MpmProjectService mpmProjectService;
    @Autowired
    private CboUserService cboUserService;

    private static final String PROJECTSOURCETYPE="MpmProjectInfo";

    /**
     * 生成App消息推送内容
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneAppContentSubmitNew(ITapService service, TapEntity entity) {
        // 审批节点实例
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String lowerFirst = Character.toLowerCase(nodeins.getSourcetype().charAt(0)) +
                nodeins.getSourcetype().substring(1);
        String serviceName = lowerFirst + "Service";
        TapService serviceBean = (TapService) SpringUtil.getBean(serviceName);
        BasicEntity selectEntity = serviceBean.queryById(ConvertUtil.convertToString(nodeins.getSourceid()));
        String projectName = mpmProjectService.queryProjectName(serviceName, selectEntity);
        String projectLevel = mpmProjectService.queryProjectLevel(serviceName, selectEntity);
        // 组织系统消息内容
        StringBufferProxy msg = new StringBufferProxy();
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispProjectLevel = datacodeCache.getValueDynamic("MpmProjectLevel", projectLevel, "code","name");
        String dispName = datacodeCache.getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        String extendfield2 = nodeins.getExtendfield2();
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务";
        }
        if (nodeins.getState() == TapApprovalNodeStateEnum.UnApproved) {
            msg.appendSingle("您提交的【{0}级项目：{1}】{2}申请,在“{3}”阶段审批已驳回，请知悉！", dispProjectLevel, projectName, dispName, extendfield2);
        } else {
            msg.appendSingle("您提交的【{0}级项目：{1}】{2}申请,在“{3}”阶段审批已通过，请知悉！", dispProjectLevel, projectName, dispName, extendfield2);
        }
        return msg.toString();
    }

    /**
     * 生成手机端系统消息主题
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneSysSubjectSubmitNew(ITapService service, TapEntity entity) {
        StringBufferProxy msg = new StringBufferProxy();
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        msg.appendSingle("销售项目审批");
        if (nodeins.getState() == TapApprovalNodeStateEnum.UnApproved) {
            msg.appendSingle("驳回");
        } else {
            msg.appendSingle("通过");
        }
        return msg.toString();
    }

    /**
     * 生成手机端系统消息内容
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneSysContentSubmitNew(ITapService service, TapEntity entity) {
        // 审批节点实例
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String lowerFirst = Character.toLowerCase(nodeins.getSourcetype().charAt(0)) +
                nodeins.getSourcetype().substring(1);
        String serviceName = lowerFirst + "Service";
        TapService serviceBean = (TapService) SpringUtil.getBean(serviceName);
        BasicEntity selectEntity = serviceBean.queryById(ConvertUtil.convertToString(nodeins.getSourceid()));
        String projectName = mpmProjectService.queryProjectName(serviceName, selectEntity);
        String projectLevel = mpmProjectService.queryProjectLevel(serviceName, selectEntity);
        // 组织系统消息内容
        StringBufferProxy msg = new StringBufferProxy();
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispProjectLevel = datacodeCache.getValueDynamic("MpmProjectLevel", projectLevel, "code","name");
        String dispName = datacodeCache.getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        String extendfield2 = nodeins.getExtendfield2();
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务";
        }
        if (nodeins.getState() == TapApprovalNodeStateEnum.UnApproved) {
            msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "您提交的【{0}级项目：{1}】{2}申请,在“{3}”阶段审批已驳回，请知悉！<br>", dispProjectLevel, projectName, dispName, extendfield2);
        } else {
            msg.appendSingle("您提交的【{0}级项目：{1}】{2}申请,在“{3}”阶段审批已通过，请知悉！<br>", dispProjectLevel, projectName, dispName, extendfield2);
        }
        String approvalopition = nodeins.getApprovalopinion();
        if (StringUtil.isEmpty(approvalopition)) {
            approvalopition = I18nUtil.getMessage("tap.common.not");
        }
        msg.appendSingle(
            "&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.cboapprovalnode.approvalcomments")
                + ":{0}<br>", approvalopition);
        msg.appendSingle(
            "&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.date.senddate") + ":{0}<br>",
            DateUtil.Now());
        return msg.toString();
    }
    /**
     * 下一节点待办消息提醒主题
     * @author: houyuhang
     * @date: 2022年10月27日
     * @params:
     */
    public String geneSysSubjectForNext(ITapService service, TapEntity entity) {
        StringBufferProxy msg = new StringBufferProxy();
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity)entity;
        DatacodeCache datacodeCache = (DatacodeCache)SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache.getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }

        msg.appendSingle("{0}通知", new Object[]{dispName});
        return msg.toString();
    }
    /**
     * @author: wudaliang
     * @date: 2021-04-22
     * @description: 立项审批通过后，给所有审批人发送消息提醒
     * @return: java.lang.String
     * @modify:
     */
    public void geneAppContentEnd(ITapService service, TapEntity entity) {

        cboMessageAppService = (ICboMessageAppService)SpringUtil.getBean("cboMessageAppService");
        cboMessageSysService = (ICboMessageSysService) SpringUtil.getBean("cboMessageSysService");
        cboApprovalNodeInsService = (CboApprovalNodeInsService)SpringUtil.getBean("cboApprovalNodeInsService");
        mpmProjectService = (MpmProjectService)SpringUtil.getBean("mpmProjectService");
        cboUserService = (CboUserService)SpringUtil.getBean("cboUserService");
        String sourceid = entity.getValue("sourceid").toString();
        MpmProjectEntity projectEntity = mpmProjectService.queryById(sourceid);
        // 项目立项审批通过，只给项目经理发送消息提醒。 传的sourcetype为MpmProjectInfo，手机端以此为标识，进行项目详情页的跳转
        //App消息
        Map inserAppMap = new HashMap<>();
        StringBufferProxy content = new StringBufferProxy();
        content.appendSingle("您提交的【'{0}'】成功立项，项目编号为：'{1}'，请于4天内召开项目启动会、完成项目任命。",
                projectEntity.getProjectname(), projectEntity.getProjectcode());
        inserAppMap.put("usercode", projectEntity.getProjectmanagercode());
        inserAppMap.put("extendfield1", "销售项目立项提醒");
        inserAppMap.put("content", content.toString());
        inserAppMap.put("sourceid", sourceid);
        inserAppMap.put("sourcetype", PROJECTSOURCETYPE);
        inserAppMap.put("sendtype", 0);
        cboMessageAppService.insertMap(inserAppMap);

        // 系统消息
        Map sysMap=new HashMap<>();
        sysMap.put("receivercode",projectEntity.getProjectmanagercode());
        sysMap.put("receivername",projectEntity.getProjectmanager());
        sysMap.put("subject","销售项目立项提醒");
        sysMap.put("content",content.toString());
        sysMap.put("sourceid",sourceid);
        sysMap.put("sourcetype",PROJECTSOURCETYPE);
        cboMessageSysService.insertMap(sysMap);
    }


}
